/**
  ******************************************************************************
  * @file    Devices_Bluetooth.c
  * @author  AnshininTakaha
  * @version V1.21
  * @date		 2021/09/29
  * @brief   The File of the Data Receive of the E22 Module.
	* @copyright (c) 2017-2021 JZI. All rights reserved.
  ******************************************************************************
  */
/* Includes ------------------------------------------------------------------*/
#include "Devices_Bluetooth.h"

#include "Devices_Command.h"
#include "Devices_LED.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/


/* Exported mention ----------------------------------------------------------*/
void REC_BlueTooth_USART_IT_Register(UART_HandleTypeDef* huartx);
void REC_BlueTooth_Mode_Setting(void);
void REC_BlueTooth_Set_Name(uint8_t *p,uint8_t len);
void BlueTooth_Send_App(char* Fake_ATCommand);
void BlueTooth_Send_AppOld(uint8_t* Fake_ATCommandOld);

/* Exported variables --------------------------------------------------------*/
/* BlueTooth Function */
BlueTooth_Fuc BlueTooth_Function = BlueTooth_FucGroundInit;
#undef BlueTooth_FucGroundInit

/* Bluetooth receiver buffer */
char REC_Bluetooth_Buffer[BlueTooth_BufferLength];

/* Exported functions --------------------------------------------------------*/
/**
  * @Data    2021-12-29 
  * @brief   Bluetooth init register.
  * @param   void
  * @retval  void
  */
void REC_BlueTooth_USART_IT_Register(UART_HandleTypeDef* huartx)
{
	/* Clear the Flag First for Prevent IDLE Flag Broken Init */
	__HAL_UART_CLEAR_IDLEFLAG(huartx);
	
	/* Open the Serial port Using Polling */
	__HAL_UART_ENABLE(huartx);
	
	/* Receive IT Enable: DMARx */
	__HAL_UART_ENABLE_IT(huartx,UART_IT_IDLE);
	
	/* Transmit IT Enable: DMATx */
	//None..
	
	/* Receive IT Enable: DMARx */
	HAL_UART_Receive_DMA(huartx, \
											(uint8_t*)REC_AT_CommandReceive, \
											AT_CommandLength);
	
	/* Link Register */
	BlueTooth_Function.BlueTooth_Register = huartx;
}



/**
 * @brief  BlueTooth Mode Setting
 * @param  void.
 * @return void.
 */
void REC_BlueTooth_Mode_Setting(void)
{
	/* Setting Command Mode */
	BlueTooth_CMD_Disable();
	HAL_Delay(10);
	
	/* Activate the Bluetooth Configuration */
	while(HAL_UART_Transmit_DMA(BlueTooth_Function.BlueTooth_Register, \
														 (uint8_t*)VRE_SEARCH, \
														 sizeof(VRE_SEARCH) - 1) != HAL_OK);
	HAL_Delay(20);
	
	/* Bluetooth PinSearch for active Bluetooth Transmit Setting */
	while(HAL_UART_Transmit_DMA(BlueTooth_Function.BlueTooth_Register, \
														 (uint8_t*)PIN_SEARCH, \
														 sizeof(PIN_SEARCH) - 1) != HAL_OK);
	HAL_Delay(20);
	
	/* Bluetooth PinSearch for active Bluetooth Transmit Setting */
	while(HAL_UART_Transmit_DMA(BlueTooth_Function.BlueTooth_Register, \
														 (uint8_t*)PIN_SEARCH, \
														 sizeof(PIN_SEARCH) - 1) != HAL_OK);
	HAL_Delay(20);
	
	/* BOARDCAST CONFIG */
	while(HAL_UART_Transmit_DMA(BlueTooth_Function.BlueTooth_Register, \
														 (uint8_t*)BT_BOARDCAST_CONFIG, \
														 sizeof(BT_BOARDCAST_CONFIG) - 1) != HAL_OK);
	HAL_Delay(20);
	
	/* BOARDCAST NAME CONFIG */
	while(HAL_UART_Transmit_DMA(BlueTooth_Function.BlueTooth_Register, \
														 (uint8_t*)BT_BOARDCAST_NAME, \
														 sizeof(BT_BOARDCAST_NAME) - 1) != HAL_OK);
	HAL_Delay(20);
	
	/* SPP CONFIG Setting */
	while(HAL_UART_Transmit_DMA(BlueTooth_Function.BlueTooth_Register	, \
														 (uint8_t*)BT_SPP_CONFIG, \
														 sizeof(BT_SPP_CONFIG) - 1) != HAL_OK);
	HAL_Delay(20);
	
	/* SPP NAME Setting */
	while(HAL_UART_Transmit_DMA(BlueTooth_Function.BlueTooth_Register, \
														 (uint8_t*)BT_SPP_NAME, \
														 sizeof(BT_SPP_NAME) - 1) != HAL_OK);
	HAL_Delay(20);
	
	/* PAIRING CONFIG */
	while(HAL_UART_Transmit_DMA(BlueTooth_Function.BlueTooth_Register, \
														 (uint8_t*)BT_PAIRING_CONFIG, \
														 sizeof(BT_PAIRING_CONFIG) - 1) != HAL_OK);
	HAL_Delay(20);
	
	/* PAIRING PASSWOED */
	while(HAL_UART_Transmit_DMA(BlueTooth_Function.BlueTooth_Register, \
														 (uint8_t*)PIN_CONF, \
														 sizeof(PIN_CONF) - 1) != HAL_OK);
	HAL_Delay(20);
	
	/* Exit Setting Command Mode */
	BlueTooth_CMD_Enable();
	
}

void REC_BlueTooth_Set_Name(uint8_t *p,uint8_t len)
{
		/* Setting Command Mode */
	BlueTooth_CMD_Disable();
		
	HAL_Delay(20);
		/* SPP CONFIG Setting */
	while(HAL_UART_Transmit_DMA(BlueTooth_Function.BlueTooth_Register	, \
														 (uint8_t*)BT_SPP_CONFIG, \
														 sizeof(BT_SPP_CONFIG) - 1) != HAL_OK);
	HAL_Delay(20);
	
	/* SPP NAME Setting */
	while(HAL_UART_Transmit_DMA(BlueTooth_Function.BlueTooth_Register, \
														 p, \
														 len - 1) != HAL_OK);
	/* Exit Setting Command Mode */
	BlueTooth_CMD_Enable();
}

/**
 * @brief  Bluetooth Data Send, Send Data to the App
 * @param  BlueTooth_Send: The Bluetooth After-TranshipmentData which Send to App.
 * @return void.
 */
void BlueTooth_Send_App(char* Fake_ATCommand)
{
	while(HAL_UART_Transmit_DMA(BlueTooth_Function.BlueTooth_Register, \
															(uint8_t*)Fake_ATCommand, \
															53) != HAL_OK);
	HAL_Delay(50);
}


/**
 * @brief  Bluetooth Data Send, Send Data to the App
 * @param  BlueTooth_Send: The Bluetooth After-TranshipmentData which Send to App.
 * @return void.
 */
void BlueTooth_Send_AppOld(uint8_t* Fake_ATCommandOld)
{
	while(HAL_UART_Transmit_DMA(BlueTooth_Function.BlueTooth_Register, \
															(uint8_t*)Fake_ATCommandOld, \
															23) != HAL_OK);
	HAL_Delay(10);
}


/* End -----------------------------------------------------------------------*/
